package com.atguigu.combine;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;

import java.util.*;

public class ConnectKeyByDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.setParallelism(2);

        DataStreamSource<Tuple2<Integer, String>> source1 = env.fromElements(
                Tuple2.of(1, "a1"),
                Tuple2.of(1, "a2"),
                Tuple2.of(2, "b"),
                Tuple2.of(3, "c")
        );
        DataStreamSource<Tuple3<Integer, String, Integer>> source2 = env.fromElements(
                Tuple3.of(1, "aa1", 1),
                Tuple3.of(1, "aa2", 2),
                Tuple3.of(2, "bb", 1),
                Tuple3.of(3, "cc", 1)
        );

        ConnectedStreams<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>> connect = source1.connect(source2);

        ConnectedStreams<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>> keyBy = connect.keyBy(i -> i.f0, i -> i.f0);

        SingleOutputStreamOperator<String> process = keyBy.process(new CoProcessFunction<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>, String>() {
            Map<Integer, List<Tuple2<Integer, String>>> s1Map = new HashMap<>();
            Map<Integer, List<Tuple3<Integer, String, Integer>>> s2Map = new HashMap<>();

            @Override
            public void processElement1(Tuple2<Integer, String> value, CoProcessFunction<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                if (!s1Map.containsKey(value.f0)) {
                    List<Tuple2<Integer, String>> list = new ArrayList<>();
                    list.add(value);
                    s1Map.put(value.f0, list);
                } else {
                    s1Map.get(value.f0).add(value);
                }

                if (s2Map.containsKey(value.f0)) {
                    for (Tuple3<Integer, String, Integer> t2 : s2Map.get(value.f0)) {
                        out.collect("t1: " + value + "===" + "t2: " + t2);
                    }
                }
            }

            @Override
            public void processElement2(Tuple3<Integer, String, Integer> value, CoProcessFunction<Tuple2<Integer, String>, Tuple3<Integer, String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
                if (!s2Map.containsKey(value.f0)) {
                    List<Tuple3<Integer, String, Integer>> list = new ArrayList<>();
                    list.add(value);
                    s2Map.put(value.f0, list);
                } else {
                    s2Map.get(value.f0).add(value);
                }

                if (s1Map.containsKey(value.f0)) {
                    for (Tuple2<Integer, String> t1 : s1Map.get(value.f0)) {
                        out.collect("t1: " + t1 + "===" + "t2: " + value);
                    }
                }
            }
        });

        process.print();

        env.execute();
    }
}
